package com.xigua._回溯;

import com.xigua.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/2/1015:54
 */
public class _257_二叉树的所有路径 {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> ans = new ArrayList<>();
        constructPaths(root, "", ans);
        return ans;
    }

    private void constructPaths(TreeNode root, String s, List<String> ans) {
        if (root != null ) {
            StringBuffer pathSB = new StringBuffer(s);
            pathSB.append(Integer.toString(root.val));
            if (root.left == null && root.right == null){
                ans.add(pathSB.toString());
            }else {
                pathSB.append("->");
                constructPaths(root.left,pathSB.toString(),ans);
                constructPaths(root.right,pathSB.toString(),ans);
            }
        }
    }


}
